package com.srmt.spring.properties;

import com.srmt.common.ratelimit.config.RateLimitConfig;
import com.srmt.common.retry.RetryConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * Document SDK Spring Boot 配置属性
 * 映射 application.yml 中的 document.* 配置
 *
 * @author SRMT Development Team
 * @since 2.0.0
 * @version 3.0.0 统一使用RateLimitConfig和RetryConfig，移除内部重复定义
 */
@ConfigurationProperties(prefix = "document")
public class DocumentProperties {

    /**
     * 是否启用 Document SDK 自动配置
     */
    private boolean enabled = true;

    /**
     * 服务配置
     */
    @NestedConfigurationProperty
    private ServiceConfig service = new ServiceConfig();

    /**
     * 平台配置集合
     */
    private Map<String, Map<String, String>> platforms = new HashMap<>();

    /**
     * 限流配置
     */
    @NestedConfigurationProperty
    private RateLimitConfig rateLimit = new RateLimitConfig();

    /**
     * 重试配置
     */
    @NestedConfigurationProperty
    private RetryConfig retry = new RetryConfig();
    
    /**
     * 服务配置
     */
    public static class ServiceConfig {
        /**
         * 使用的平台: feishu, dingtalk, wechat-work 等
         */
        private String platform = "feishu";
        
        /**
         * 请求超时时间（毫秒）
         */
        private int timeout = 10000;
        
        /**
         * 是否在DEBUG级别记录请求日志
         */
        private boolean logReqAtDebug = true;
        
        /**
         * 缓存配置
         */
        @NestedConfigurationProperty
        private CacheConfig cache = new CacheConfig();
        
        public static class CacheConfig {
            private boolean enabled = true;
            private int size = 1000;
            private int ttlMinutes = 60;
            
            // Getters and Setters
            public boolean isEnabled() {
                return enabled;
            }
            
            public void setEnabled(boolean enabled) {
                this.enabled = enabled;
            }
            
            public int getSize() {
                return size;
            }
            
            public void setSize(int size) {
                this.size = size;
            }
            
            public int getTtlMinutes() {
                return ttlMinutes;
            }
            
            public void setTtlMinutes(int ttlMinutes) {
                this.ttlMinutes = ttlMinutes;
            }
        }
        
        // Getters and Setters
        public String getPlatform() {
            return platform;
        }
        
        public void setPlatform(String platform) {
            this.platform = platform;
        }
        
        public int getTimeout() {
            return timeout;
        }
        
        public void setTimeout(int timeout) {
            this.timeout = timeout;
        }
        
        public boolean isLogReqAtDebug() {
            return logReqAtDebug;
        }
        
        public void setLogReqAtDebug(boolean logReqAtDebug) {
            this.logReqAtDebug = logReqAtDebug;
        }
        
        public CacheConfig getCache() {
            return cache;
        }
        
        public void setCache(CacheConfig cache) {
            this.cache = cache;
        }
    }
    
    
    // Main class getters and setters
    public boolean isEnabled() {
        return enabled;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    public ServiceConfig getService() {
        return service;
    }
    
    public void setService(ServiceConfig service) {
        this.service = service;
    }
    
    public Map<String, Map<String, String>> getPlatforms() {
        return platforms;
    }
    
    public void setPlatforms(Map<String, Map<String, String>> platforms) {
        this.platforms = platforms;
    }
    
    public RateLimitConfig getRateLimit() {
        return rateLimit;
    }

    public void setRateLimit(RateLimitConfig rateLimit) {
        this.rateLimit = rateLimit;
    }

    public RetryConfig getRetry() {
        return retry;
    }

    public void setRetry(RetryConfig retry) {
        this.retry = retry;
    }
    
    /**
     * 获取当前平台的配置
     */
    public Map<String, String> getCurrentPlatformConfig() {
        return platforms.get(service.getPlatform());
    }
}